package com.yasenagat

import java.io.File

/**
 * Created by yasenagat on 17/5/26 Time 下午4:23.
 */

/**DTO 参数默认值**/
data class Customer(val name: String, val email: String, val age: Int = 18)

fun main(args: Array<String>) {

    var jack = Customer("jack", "hi@gmail.com", 11)
    println(jack)
    println(jack.toString())
    println(jack.name)
    println(jack.email)
    println(jack.component1())
    println(jack.component2())
    println(jack.component3())

    var tom = Customer("tom", "tomcat@gmail.com")
    println(tom)

    defaultValue()
    defaultValue(1)

    var nameList = listOf<String>("tom", "luck")

    nameList.filter { it.contains("t") }
            .forEach { println(it) }

    var numberList = listOf<Int>(1, 23, 4, 5, 6)
    numberList.filter { it > 5 }
            .forEach { println(it) }

    var x: Any = "hello";
    when (x) {
        is Int -> println("$x is int")
        is String -> println("$x is string")
        else -> println("other type")
    }

    var map_ = mapOf<String, String>("a" to "1", "b" to "2", "c" to "3")

    for ((k, v) in map_) {
        println("$k = $v")
    }

    //val=只读的数据，不可修改
    val name = "abc"
    val list = listOf("a", "b", "c")
    val map = mapOf("a" to 1, "b" to 2, "c" to 3)

    println(list.get(0))
    println(map.get("a"))
    println(map["a"])

    println(p)
    println(count)

    p.myFunc()

    //单利模式
    println(Resource.name + Resource.age)

    val files = File("/Users/yasenagat").listFiles()
    println("filesize = ${files?.size}")
    files.forEach { println(it.name) }

    //判断!=null简写
    var strNull: String? = "abc"
    println("strNull length ${strNull?.length}")

    //if !=null else
    strNull = null
    println(strNull?.length ?: "length is 0")

    //表达式取值,if == null 判断
    var data = mapOf("email" to null)

//    var email = data.get("email") ?: throw IllegalArgumentException("email is empty")
    var email = data.get("email") ?: println("email is empty")

    //如果!=null 执行下面的语句

    var address = "jinan"
    address?.let { println("welcome to jinan") }

    //when来return结果
    transformColor2Code("red")
//    transformColor2Code("black")//run else

    //try catch 表达式
    test()

    //if表达式
    println(parseNum(1))
    println(parseNum(3))
    println(parseNum2(2))
    println(parseNum2(3))

    println(myName())

    //使用with调用一个对象的多个方法,不用一个一个的对象.方法()调用了。
    val myTurtle = Turtle()
    with(myTurtle) { //draw a 100 pix square
        penDown()
        for (i in 1..4) {
            forward(100.0)
            turn(90.0)
        }
        penUp()
    }


    val b: Boolean? = false
    if (b == true) {

    } else {
// `b` is false or null
    }

}

class Turtle {
    fun penDown() {
    }

    fun penUp() {
    }

    fun turn(degrees: Double) {
    }

    fun forward(pixels: Double) {
    }
}


//表达式方法
fun myName() = "jack bill"

//同上面的意思
fun myName2(): String {
    return "tom cat"
}

fun parseNum(num: Int): String {

    var result = if (num == 1) {
        "one"
    } else if (num == 2) {
        "two"
    } else {
        "other"
    }
    return result
}

//表达式可以不需要写return
fun parseNum2(num: Int): String = if (num == 1) {
    "one"
} else if (num == 2) {
    "two"
} else {
    "other"
}

fun getResult(): Int {
    return 3 + 1
}

fun test() {
    val result = try {
        getResult()
    } catch (e: ArithmeticException) {
        throw IllegalStateException(e)
    }
// Working with result

    println(result)
}

fun transformColor2Code(color: String): Int {
    return when (color) {
        "red" -> 0
        "blue" -> 1
        "green" -> 2
        else -> throw IllegalArgumentException("not support this color")
    }
}

object Resource {
    val name = "Name"
    var age = 3
}

//给string类型扩展方法
fun String.myFunc() {
    println(this.replace("a", "x", true))
}

//懒加载
val p: String by lazy<String> {
// compute the string
    "abc" + 3
}

//懒加载
val count: Double by lazy<Double> {
    3 * 3 * 10 * 1.2
}


fun defaultValue(a: Int = 3) {
    println("a is $a")
}